import io.vertx.core.AbstractVerticle
import io.vertx.core.AsyncResult
import io.vertx.core.Vertx
import io.vertx.redis.client.*


/*
*
*   fun withPwd() {
        val vertx = Vertx.vertx()

        val client = Redis.createClient(
            vertx, //redis://[:password@]host[:port][/db-number]
            //unix://[:password@]/domain/docker.sock[?select=db-number]
            "redis://:123456@192.168.20.130:6379/1"
        )
        val redis: RedisAPI = RedisAPI.api(client)

        redis["mykey"]
            .onSuccess { value: Response? -> }

        Redis.createClient(
            vertx, //redis://[:password@]host[:port][/db-number]
            //unix://[:password@]/domain/docker.sock[?select=db-number]
            "redis://:123456@192.168.20.130:6379/1"
        )
            .connect()
            .onSuccess { conn: RedisConnection? ->
                println("conn Success")

            }
    }

    private fun conn(): RedisAPI {
        val vertx = Vertx.vertx()
        val client = Redis.createClient(
            vertx, //redis://[:password@]host[:port][/db-number]
            //unix://[:password@]/domain/docker.sock[?select=db-number]
            "redis://:123456@192.168.20.130:6379/1"
        ).apply {
            connect().onSuccess { conn: RedisConnection? ->
                println("conn Success")
            }
                .onFailure {
                    println("conn onFailure")
                    return@onFailure

                }
        }
        return RedisAPI.api(client)
    }

    fun getData() {
        val api = conn()
        api.get("name")?.onSuccess { v: Response ->
            println("redis连接成功")
            api.close()
        }?.onFailure { t: Throwable ->

            println("redis连接失败");
            println(t)

        }

    }
* */

class MyVertxRedis : AbstractVerticle() {
    @Throws(Exception::class)
    override fun start() {
        //设置redis信息

        getData()
    }

    fun dddd() {
        //创建redisclient实例
        val client = Redis.createClient(vertx, "redis://:123456@192.168.20.130:6379/1")
        val api = RedisAPI.api(client)
        api["name", { res: AsyncResult<Response> ->
            if (res.succeeded()) {
                println("redis连接成功")
            } else {
                println("redis连接失败")
                res.cause().printStackTrace()
            }
            //关闭连接
            api.close()
            vertx.close()
        }]

        //因为vertx是异步的，如果在这里关闭的话，可能造成上面还没执行完成连接就关闭了，造成异常
        /*api.close();
        vertx.close();*/
    }

    private fun conn(): RedisAPI {
//        val vertx = Vertx.vertx()
        val client = Redis.createClient(
            vertx, //redis://[:password@]host[:port][/db-number]
            //unix://[:password@]/domain/docker.sock[?select=db-number]
            "redis://:123456@192.168.20.130:6379/1"
        ).apply {
            connect().onSuccess { conn: RedisConnection? ->
                println("conn Success")
            }
                .onFailure {
                    println("conn onFailure")
                    return@onFailure

                }
        }
        return RedisAPI.api(client)
    }

    fun getData() {
        val redis = conn()


//添加
        redis.getset("name", "14").onSuccess { resp ->
            println("getset(\"name\"):$resp")

        }
        redis.getset("name1", "25").onSuccess { resp ->
            println("getset(\"name\"):$resp")

        }
        redis.hget("name", "ssss").onSuccess { resp ->
            println("hget(\"name\"):$resp")
        }
//查找
        redis.get("name").onSuccess { resp ->
            println("get(\"name\"):$resp")
        }


        redis.del(listOf("name","name1")).onSuccess { resp ->
            println("del(\"name\"):$resp")
        }



    }

    /**
     * 高可用性模式
     * ，会与服务器建立额外的连接，并且在幕后，客户端将侦听来自哨兵的事件。
     * 当哨兵通知我们切换了主控时，就会向客户端发送一个异常，您可以决定下一步该做什么
     */
    fun ddd() {
        Redis.createClient(
            vertx,
            RedisOptions()
                .setType(RedisClientType.SENTINEL)
                .addConnectionString("redis://127.0.0.1:5000")
                .addConnectionString("redis://127.0.0.1:5001")
                .addConnectionString("redis://127.0.0.1:5002")
                .setMasterName("sentinel7000")
                .setRole(RedisRole.MASTER)
        )
            .connect()
            .onSuccess { conn: RedisConnection ->
                conn.send(Request.cmd(Command.INFO))
                    .onSuccess { info: Response? -> }
            }
    }

    /**
     * 集群模式
     * 此列表将用于向集群询问当前配置，这意味着如果列出的任何成员不可用，它将被跳过
     */
    fun ff() {
        val options = RedisOptions()
            .addConnectionString("redis://127.0.0.1:7000")
            .addConnectionString("redis://127.0.0.1:7001")
            .addConnectionString("redis://127.0.0.1:7002")
            .addConnectionString("redis://127.0.0.1:7003")
            .addConnectionString("redis://127.0.0.1:7004")
            .addConnectionString("redis://127.0.0.1:7005")
    }

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            Vertx.vertx().deployVerticle(MyVertxRedis())
        }
    }
}